package com.mail.portal.modular.controller;

import com.mail.portal.common.controller.BaseController;
import com.mail.portal.modular.model.News;
import com.mail.portal.modular.model.ProductCategory;
import com.mail.portal.modular.service.INewsService;
import com.mail.portal.modular.service.IProductCategoryService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.websocket.server.PathParam;
import java.util.List;


/**
 * 创建人： Shi Heng
 * 创建时间：2018-04-11 12:19
 * QQ：374696376
 * Description：
 */
@Controller
@RequestMapping("/jhcb/news")
public class NewsController extends BaseController {

    private final String PREFIX = "/jhcb/product";

    @Resource
    private IProductCategoryService productCategoryService;

    @Resource
    private INewsService newsService;

    @RequestMapping("/category")
    @ResponseBody
    public List<ProductCategory> queryAll(){
        return productCategoryService.selectAll();
    }

    @RequestMapping("/type/{category}.html")
    public String category(@PathVariable("category") String category, Model model) {
        List<News> newss = newsService.queryByCategory(category);
        model.addAttribute("newss", newss);
        List<ProductCategory> categories = productCategoryService.selectAll();
        model.addAttribute("categories", categories);
        return "/news_list.html";
    }

    /**
     * 新闻列表
     */
    @RequestMapping("/news_list.html")
    public String newsList(Model model,@PathParam("page") Integer page) {
        page = null == page ? 1 : page;
        List<News> newss = newsService.selectAll(page,10000);
        model.addAttribute("newss", newss);
        List<ProductCategory> categories = productCategoryService.selectAll();
        model.addAttribute("categories", categories);
        return "/news_list.html";
    }

    /**
     * 新闻详情
     *
     * @return
     */
    @RequestMapping("/news_detail/{id}.html")
    public String newsDetail(@PathVariable("id") Integer id,Model model) {
        News news = newsService.selectByPrimaryKey(id);
        model.addAttribute("news", news);
        List<ProductCategory> categories = productCategoryService.selectAll();
        model.addAttribute("categories", categories);
        return "/news_detail.html";
    }

}
